package com.managementSystem.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Registerview {
	private final StaffAppovalBeanServiceAsync register = GWT.create(StaffAppovalBeanService.class);
	Label IdLabel = new Label("ID:");
	TextBox IdBox = new TextBox();
	Label fNameLabel = new Label("FirstName:");
	TextBox fNameBox = new TextBox();
	Label lNameLabel = new Label("LastName:");
	TextBox lNameBox = new TextBox();
	Label uNameLabel = new Label("UserName:");
	TextBox uNameBox = new TextBox();
	Label emailLabel = new Label("Email:");
	TextBox emailBox = new TextBox();
	Label password1Label = new Label("Password:");
	TextBox password1Box = new TextBox();
	Label password2Label = new Label("Password(Re-type):");
	TextBox password2Box = new TextBox();
	Label pNumLabel = new Label("Primary Number:");
	TextBox pNumBox = new TextBox();
	Label sNumLabel = new Label("Secondary number:");
	TextBox sNumBox = new TextBox();
	Button button = new Button("Submit");

	Grid grid = new Grid(10, 2);
	@SuppressWarnings("deprecation")
	public void loadRegisterview() {
		RootPanel.get().clear();
		grid.setWidget(0, 0, IdLabel);
		grid.setWidget(0, 1, IdBox);
		grid.setWidget(1, 0, fNameLabel);
		grid.setWidget(1, 1, fNameBox);
		grid.setWidget(2, 0, lNameLabel);
		grid.setWidget(2, 1, lNameBox);
		grid.setWidget(3, 0, uNameLabel);
		grid.setWidget(3, 1, uNameBox);
		grid.setWidget(4, 0, emailLabel);
		grid.setWidget(4, 1, emailBox);
		grid.setWidget(5, 0, password1Label);
		grid.setWidget(5, 1, password1Box);
		grid.setWidget(6, 0, password2Label);
		grid.setWidget(6, 1, password2Box);
		grid.setWidget(7, 0, pNumLabel);
		grid.setWidget(7, 1, pNumBox);
		grid.setWidget(8, 0, sNumLabel);
		grid.setWidget(8, 1, sNumBox);
		grid.setWidget(9, 1, button);
		RootPanel.get().add(grid);

		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				System.out.println(password1Box.getTitle().toString()+" "+password2Box.getTitle().toString());
				if (password1Box.getTitle().toString().equals(password2Box.getTitle().toString())) {
					grid.setVisible(false);
					RootPanel.get().add(new Label("Thanks for your submission."));
					StaffApprovalBean newRegister =new StaffApprovalBean();
					register.addNewApproval(newRegister, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Registration cannot be sent");
							
						}

						@Override
						public void onSuccess(Void result) {
							Window.alert("Registration sent");
							
						}
						
					});
					
				} else {
					Window.alert("Password mis-match!"+password2Box.getText().toString()+" "+password1Box.getText().toString());
				}
			}
		});

	}

}
